package sz.szsmk.citizencard.util;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.text.TextUtils;

import com.umeng.message.UmengMessageService;
import com.umeng.message.entity.UMessage;

import org.json.JSONObject;

import sz.szsmk.citizencard.R;

/**
 * Copyright (c)2021/4/28
 * author: hxh
 * created on: 2021/4/28 17:34
 * description:
 */

public class UmengIntentService extends UmengMessageService {
    NotificationManager manager;
    int id;

    public void getNotification(Context context, String title, String msg, String msgBody) {

        manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        id = (int) (System.currentTimeMillis() / 1000);

        //判断8.0，若为8.0型号的手机进行创下一下的通知栏
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_HIGH);
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
            Notification.Builder builder = new Notification.Builder(context, "channel_id");
            manager.notify(id, builder.build());
        } else {
            Notification.Builder builder = new Notification.Builder(context);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                manager.notify(id, builder.build());
            }
        }
    }

    //取消通知
    public void cancelNotify() {
        manager.cancel(id);
    }

    @Override
    public void onMessage(Context context, Intent intent) {
        try {
//            Intent data = new Intent(intent);
//            data.setClass(context, DialogActivity.class);
//            data.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//需为Intent添加Flag：Intent.FLAG_ACTIVITY_NEW_TASK，否则无法启动Activity。
//            context.startActivity(data);
            //可以通过MESSAGE_BODY取得消息体

            final String message = intent.getStringExtra("body");
            if (TextUtils.isEmpty(message)) {
                return;
            }
            final UMessage msg = new UMessage(new JSONObject(message));
            getNotification(context, msg.title, msg.text, message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

